var path = require('path')
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var projectRoot = path.resolve(__dirname, './');
var utils = require('./utils')
assetPath=utils.assetPath(process.argv[2])
module.exports = {
    //devtool: 'eval-source-map',
    entry: {
        //app: __dirname + "/main.js"
        app: ["babel-polyfill", __dirname + "/src/main.js"]
    }, //已多次提及的唯一入口文件
    output: {
        path: __dirname + "/public", //打包后的文件存放的地方
        filename: assetPath("js/[name][hash:7].js"), //打包后输出文件的文件名
        publicPath:  '/',
        chunkFilename: assetPath("js/[name][hash:7].js")
    },
    resolve: {
        extensions: ['', '.js', '.vue', '.css'],
        fallback: [path.join(__dirname, '../node_modules')],
        alias: {
            'src': path.resolve(__dirname, './src'),
            'assets': path.resolve(__dirname, './public')
        }
    },
    module: { //在配置文件里添加JSON loader
        loaders: [{
            test: /\.json$/,
            loader: "json"
        }, {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract("style-loader", "css-loader")
        }, {
            test: /\.scss$/,
            loader: "style!css!sass"
        }, {
            test: /\.(less)$/,
            loader: "style!css!less"
        }, {
            test: /\.js$/,
            loader: 'babel',
            include: projectRoot,
            exclude: /node_modules/
        }, {
            test: /\.vue$/,
            loader: 'vue'
        }, {
            test: /\.html$/,
            loader: 'vue-html'
        }, {
            test: /\.(png|jpg|gif|svg)(\?.*)?$/,
            loader: 'url',
            query: {
                limit: 1000,
                name: assetPath('img/[name].[hash:7].[ext]')
            }
        }, {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
            loader: 'url',
            query: {
                limit: 10000,
                name: assetPath('fonts/[name].[hash:7].[ext]')
            }
        }]
    },
    vue: {
        loaders: {
            js: 'babel',
            css: ExtractTextPlugin.extract("css"),
            sass: ExtractTextPlugin.extract("style!css!sass,scss"),
        },
    },
    plugins: [
        new webpack.BannerPlugin("Copyright Flying Unicorns inc."),
        new ExtractTextPlugin(assetPath("css/styles.css")),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),
        new HtmlWebpackPlugin({
            filename: process.env.NODE_ENV === 'testing' ? assetPath('index.html') : assetPath('index.html'),
            template: 'index.html',
            inject: true,
            minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeAttributeQuotes: true
                // more options:
                // https://github.com/kangax/html-minifier#options-quick-reference
            }
        })
    ],
    /*devServer: {
     contentBase: "./public", //本地服务器所加载的页面所在的目录
     colors: true, //终端中输出结果为彩色
     historyApiFallback: true, //不跳转
     inline: true //实时刷新
     }*/
}
